package org.cy.spring.learning.rpc.comm.network;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * create date: 2019/10/23 11:24
 *
 * @author cao.yong
 */
public class SocketClient implements NetworkClient {

    @Override
    public Response request(Request request) {
        Response response = null;
        try {
            Socket socket = new Socket("127.0.0.1", 8888);

            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(request);
            oos.flush();

            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            response = (Response) ois.readObject();

            ois.close();
            oos.close();

            socket.close();

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return response;
    }
}
